/**
 * @(#) ReqVacationProxy.as
 *
 * @date		2009. 03. 10.
 * @version		1.0
 * @author		Shin, Yunwoo
 * @description	 휴가신청 Proxy - Proxy
 * 				휴가 신청 리스트를 조회하고 저장, 수정, 삭제한다.
 */
package com.skt.common_hr.work_sac.model
{
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.work_sac.WorkFacade;
	import com.skt.common_hr.work_sac.model.vo.ReqVacationVO;
	import com.wcl.utils.DateUtil;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
		
	public class ReqVacationProxy extends CommonProxy
	{
		public static const NAME:String = "ReqVacationProxy";
		
		public var reqVacationList:ArrayCollection;
		public var outKey:String;
		
		/**
		 * 생성자
		 * 
		 * @param
		 * @return void
		 */
		public function ReqVacationProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
	
		
		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 * 
		 * @param pernr 사번
		 * @return void
		 */
		public function load(key:String, isInsertFromCong:Boolean=false):void
		{
			var param:Object = new Object();
			param["I_GWAREKEY"] 		= key;
			
			//20110630 경조휴가 조회 안되서 변경함.			
			if(isInsertFromCong == true) {
				loadData("Z_HR_0047", param, resultLoad2, faultLoad);	
			}
			else {
				loadData("Z_HR_0028", param, resultLoad, faultLoad);	
			}
			 
						 
			/*
			var loader:IDataLoader = DataLoaderFactory.getDataLoader("Z_HR_0019");
			CursorManager.setBusyCursor();
			var param:Object = new Object();
			param["I_PERNR"] 		= pernr;
			param["I_BEGDA"]		= beginDate;
			param["I_ENDDA"]		= endDate;
			param["I_PROC_TYPE"]	= "S";			//'D'삭제, 'S'조회
			loader.load(param, resultLoad, faultLoad); 
			*/
		}
		
		
		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 * 
		 * @param pernr 사번
		 * @return void
		 */
		public function del(table:Array):void
		{
			var param:Object = new Object();
			//param["I_PERNR"] 		= pernr;
			//param["I_BEGDA"]		= "";
			//param["I_ENDDA"]		= "";
			//param["I_PROC_TYPE"]	= "D";			//'D'삭제, 'S'조회
			//param["I_WSTATUS"]		= "4";
			//param["I_DGBN"]			= "1";
			//param["I_GWAREKEY"]		= key;
			//trace("delete: " + key);
			var tables:Object = new Object;
			tables["ITAB"] = table;
			
			saveData("Z_HR_0069", param, tables, resultDelete, faultDelete); 
		}
		
		
		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 * 
		 * @param pernr 사번
		 * @return void
		 */
		public function InsertVacation(type:String, table:Array, html:String=""):void
		{
			var param:Object = new Object();
			//param["I_PROC_TYPE"] 	= "I";				//처리구분(I:신규,M:수정)
			//param["I_WSTATUS"]		= type;			//결재유형(1:임시저장,2:승인요청)
			//param["I_HTML"]			= html; //buildHTML(table);
			//param["I_XML"]			= buildXML();
			
			var tables:Object = new Object();
			
			tables["ITAB"] = toJSONTable(table);
			
			if(Variables.company == "SAC" ) {
				saveData("Z_HR_0022", param, tables, resultInsert, faultInsert);	
			} else {
				saveData("Z_HR_0022", param, tables, resultInsert_stc, faultInsert);
			}
		}
		
		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 * 
		 * @param pernr 사번
		 * @return void
		 */
		public function modifyVacation(type:String, table:Array, html:String=""):void
		{
			var param:Object = new Object();
			param["I_PROC_TYPE"] 	= "I";				//처리구분(I:신규,M:수정)
			param["I_WSTATUS"]		= type;			//결재유형(1:임시저장,2:승인요청)
			param["I_HTML"]			= html; //buildHTML(table);
			//param["I_XML"]			= buildXML();
			trace(param["I_XML"]);
			var tables:Object = new Object();
			tables["ITAB"] = table;

			saveData("Z_HR_0022", param, tables, resultInsert, faultInsert);			
		}
		




		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public function resultLoad(event:ResultEvent):void
		{
			if(retCode==0) {  
				reqVacationList = parseVacationList(event.result.ITAB);
				facade.sendNotification(WorkFacade.LOAD_REQ_VACATION_COMPLETE, this);
			}
			else {
				facade.sendNotification(WorkFacade.LOAD_REQ_VACATION_FAILED, this);
			}
		}
		// 20110630 경조휴가일경우 . 
		public function resultLoad2(event:ResultEvent):void
		{
			if(retCode==0) {  
				reqVacationList = parseVacationList2(event.result.ITAB);
				facade.sendNotification(WorkFacade.LOAD_REQ_VACATION_COMPLETE, this);
			}
			else {
				facade.sendNotification(WorkFacade.LOAD_REQ_VACATION_FAILED, this);
			}
		}
				
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function faultLoad(event:FaultEvent):void
		{
			facade.sendNotification(WorkFacade.LOAD_REQ_VACATION_FAILED, this);
		}
		
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public function resultDelete(event:ResultEvent):void
		{
			if(retCode==0) {  
				//vacationList = parseVacationList(event.result.CALTAB2);
				trace("deleted....vacation");
				facade.sendNotification(WorkFacade.DELETE_REQ_VACATION_COMPLETE, this);
			}
			else {
				trace("failed....delete, vacation");
				facade.sendNotification(WorkFacade.DELETE_REQ_VACATION_FAILED, this);
			}
		}
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function faultDelete(event:FaultEvent):void
		{
			facade.sendNotification(WorkFacade.DELETE_REQ_VACATION_FAILED, this);
		}
		
		
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public function resultInsert(event:ResultEvent):void
		{
			if(retCode==0) {  
				outKey = event.result["O_GWAREKEY"];
				facade.sendNotification(WorkFacade.INSERT_REQ_VACATION_COMPLETE, this);
			}
			else {
				facade.sendNotification(WorkFacade.INSERT_REQ_VACATION_FAILED, this);
			}
		}

		public function resultInsert_stc(event:ResultEvent):void
		{
			if(retCode==0) {  
				outKey = event.result["O_GWAREKEY"];
				facade.sendNotification(WorkFacade.INSERT_REQ_VACATION_COMPLETE, this);
			}
			else {
				facade.sendNotification(WorkFacade.INSERT_REQ_VACATION_FAILED, this);
			}
		}		
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function faultInsert(event:FaultEvent):void
		{
			facade.sendNotification(WorkFacade.INSERT_REQ_VACATION_FAILED, this);
		}
		
		
		
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Data Parsing functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 전달된 result 객체를 분석하여 Value Object를 생성
		 * 
		 * @param
		 * @return 제증명서 종류 Code list
		 */
		private function parseVacationList(srcArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();
			
			for each(var rec:Object in srcArr) 
			{
				var vo:ReqVacationVO = new ReqVacationVO();
				vo.SEQNO		= rec["SEQNO"];
				vo.PERNR 		= rec["PERNR"];
				vo.DGBN			= rec["DGBN"];
				vo.AWART		= rec["AWART"];
				vo.AWATX		= rec["ATEXT"];
				vo.BEGDA		= DateUtil.stringToDateTime(rec["BEGDA"]);
				vo.ENDDA		= DateUtil.stringToDateTime(rec["ENDDA"]);
				vo.BEGUZ		= DateUtil.stringToDateTime(rec["BEGUZ"]);
				vo.ENDUZ		= DateUtil.stringToDateTime(rec["ENDUZ"]);
				vo.WSTATUS		= rec["WSTATUS"];
				vo.IPERNR		= rec["IPERNR"];
				vo.PERNR_OLD	= rec["PERNR_OLD"];
				vo.BEGDA_OLD	= DateUtil.stringToDateTime(rec["BEGDA_OLD"]);
				vo.BEGUZ_OLD	= DateUtil.stringToDateTime(rec["BEGUZ_OLD"]);
				vo.DUPLICATED	= rec["DUPLICATED"];
				vo.GWAREKEY		= rec["GWAREKEY"];
				vo.TEXT1		= rec["TEXT1"];
				vo.TEXT2		= rec["TEXT2"];
				
				vo.OT011		= rec["OT011"];
				vo.OT012		= rec["OT012"];
				vo.OT021		= rec["OT021"];
				vo.OT022		= rec["OT022"];
				
			
				//2013.06.21 j.t추가
				vo.APM			= rec["APM"];
				
//				if(Variables.company=='SAC') {
//					vo.OT023		= rec["OT023"];	
//				}
				vo.OT023		= rec["OT023"];
				retArr.addItem(vo);
			}
			return retArr;	
		}

		// 경조휴가일경우..
		private function parseVacationList2(srcArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();
			
			for each(var rec:Object in srcArr) 
			{
				var vo:ReqVacationVO = new ReqVacationVO();
				vo.CODYS		= rec["CODYS"]; // 휴가일수
				vo.BEGDA		= DateUtil.stringToDateTime(rec["COBEG"]);  
				vo.ENDDA		= DateUtil.stringToDateTime(rec["COEND"]);  
				vo.TEXT1		= rec["ZTEXT"];  
				vo.WSTATUS		= rec["WSTATUS"];
				//vo.WSTATX		= rec["WSTATX"];
				vo.AWATX		= '경조휴가';  
				
				retArr.addItem(vo);
			}
			return retArr;	
		}
		private function toJSONTable(srcTable:Array):Array
		{
			var jsonTable:Array = new Array;
			for(var i:int=0; i<srcTable.length; i++) {
				var vo:ReqVacationVO = srcTable[i];
				var jsonObj:Object = new Object;
				jsonObj["SEQNO"] = vo.SEQNO;
				jsonObj["PERNR"] = vo.PERNR;
				
				//2013.06.21 j.t추가
				jsonObj["APM"]	 = vo.APM;
				
				jsonObj["DGBN"]  = vo.DGBN;
				jsonObj["AWART"] = vo.AWART;
				jsonObj["AWATX"] = vo.AWATX;
				jsonObj["BEGDA"] = DateUtil.toBasicString(vo.BEGDA);
				jsonObj["ENDDA"] = DateUtil.toBasicString(vo.ENDDA);
				jsonObj["BEGUZ"] = DateUtil.toBasicTimeString(vo.BEGUZ);
				jsonObj["ENDUZ"] = DateUtil.toBasicTimeString(vo.ENDUZ);
				jsonObj["WSTATUS"] = vo.WSTATUS;
				jsonObj["IPERNR"] = vo.IPERNR;
				jsonObj["PERNR_OLD"] = vo.PERNR_OLD;
				jsonObj["BEGDA_OLD"] = DateUtil.toBasicTimeString(vo.BEGDA_OLD);
				jsonObj["BEGUZ_OLD"] = DateUtil.toBasicTimeString(vo.BEGUZ_OLD);
				jsonObj["DUPLICATED"] = vo.DUPLICATED;
				jsonObj["GWAREKEY"] = vo.GWAREKEY;
				jsonObj["TEXT1"] = vo.TEXT1;
				jsonObj["TEXT2"] = vo.TEXT2;
				
				jsonTable.push(jsonObj);
			}
			return jsonTable;
		}



		/*
		private function buildHTML(table:Array):String
		{
			var htmlAll:String = templateHTML;
			var htmlBlock:String = "";
			//<TR>
			//<TD class=rowdata1 width=70 height=20>&nbsp;연차</TD>
			//<TD class=rowdata1 width=70 height=20>&nbsp;2008-09-01</TD>
			//<TD class=rowdata1 width=70 height=20>&nbsp;2008-09-05</TD>
			//<TD class=rowdata1 width=70 height=20>&nbsp;오후</TD>
			//<TD class=rowdata width=344 height=20>&nbsp;개인 refresh</TD></TR></TBODY></TABLE></TD></TR>
			for each(var vo:ReqVacationVO in table)
			{
				var htmlLine:String = "<TR>\n"
				htmlLine += "<TD style='background: #fdfdf3; text-align: center; font-size: 9pt; border-bottom-color : #000000; border-bottom-style : solid; border-bottom-width : 1px; border-right-color : #000000; border-right-style : solid; border-right-width : 1px;' width=80 height=20>&nbsp;" + StringUtil.NVL(vo.AWATX) + "</TD>\n";
				htmlLine += "<TD style='background: #fdfdf3; text-align: center; font-size: 9pt; border-bottom-color : #000000; border-bottom-style : solid; border-bottom-width : 1px; border-right-color : #000000; border-right-style : solid; border-right-width : 1px;' width=90 height=20>&nbsp;" + DateUtil.toDefaultString(vo.BEGDA) + "</TD>\n";
				htmlLine += "<TD style='background: #fdfdf3; text-align: center; font-size: 9pt; border-bottom-color : #000000; border-bottom-style : solid; border-bottom-width : 1px; border-right-color : #000000; border-right-style : solid; border-right-width : 1px;' width=90 height=20>&nbsp;" + DateUtil.toDefaultString(vo.ENDDA) + "</TD>\n";
				if(vo.APM!=null) {
					htmlLine += "<TD style='background: #fdfdf3; text-align: center; font-size: 9pt; border-bottom-color : #000000; border-bottom-style : solid; border-bottom-width : 1px; border-right-color : #000000; border-right-style : solid; border-right-width : 1px;' width=70 height=20>&nbsp;" + ((vo.APM=="A") ? "오전" : "오후") + "</TD>\n";
				}
				else {
					htmlLine += "<TD style='background: #fdfdf3; text-align: center; font-size: 9pt; border-bottom-color : #000000; border-bottom-style : solid; border-bottom-width : 1px; border-right-color : #000000; border-right-style : solid; border-right-width : 1px;' width=70 height=20>&nbsp;</TD>\n";
				}
				htmlLine += "<TD style='background: #fdfdf3; text-align: left; font-size: 9pt; border-bottom-color : #000000; border-bottom-style : solid; border-bottom-width : 1px; border-right-color : #000000; border-right-style : solid;  border-right-width : 1px;' width=270 height=20>&nbsp;" + StringUtil.NVL(vo.TEXT1) + "</TD></TR>\n";
				htmlBlock += htmlLine;
			}
			
			var ptnBlock:RegExp = /__RECORD_BLOCK__/; 
			var ptnMember:RegExp = /__MEMBER__/;
			htmlAll = htmlAll.replace(ptnMember, GlobalVars.myName);
			return htmlAll.replace(ptnBlock, htmlBlock);
		}
		

		private function buildXML():String
		{
			var tmpXML:XML = new XML("<header>"+
						"<line>"+
							"<line_author></line_author>"+
							"<line_reviewer seq='1'></line_reviewer>"+
							"<line_reviewer seq='2'></line_reviewer>"+
							"<line_approver></line_approver>"+
							"<line_docharger></line_docharger>"+
						"</line>"+
						"<inf>"+
						    "<inf_type>HR</inf_type>"+
						    "<inf_formkind>IF_HR_001</inf_formkind>"+
						    "<inf_keycode>__keycode__</inf_keycode>"+
						    "<inf_subject>휴가 품의</inf_subject>"+
						    "<inf_createdt>작성일자 : yyyy-mm-dd mm:hh:ss</inf_createdt>"+
						    "<inf_htmlnm>__filename__</inf_htmlnm>"+
						"</inf>"+
					"</header>");
			tmpXML.line.line_author = StringUtil.trimFront(GlobalVars.myPernr, "0");
			tmpXML.line.line_approver = GlobalVars.myBossCode;
			tmpXML.line.line_docharger = GlobalVars.getModuleManager("WORK");
			tmpXML.inf.inf_createdt = DateUtil.today();
			return "<?xml version='1.0' encoding='utf-8'?>" + tmpXML.toXMLString();
		}
		*/
	}
}